package com.jeesite.modules.member.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jeesite.common.lang.StringUtils;
import com.jeesite.common.service.CrudService;
import com.jeesite.common.web.http.HttpClientUtils;
import com.jeesite.modules.member.dao.MemberApiDao;
import com.jeesite.modules.member.entity.MemberApi;
import org.springframework.stereotype.Service;

/**
 * @author Mr Wu
 * @date 2025/10/28
 */
@Service
public class MemberApiService extends CrudService<MemberApiDao, MemberApi> {

    /**
     * 获取微信openid
     * */
    public String getOpenid(String code){
        try{
            String result = HttpClientUtils.get("https://api.weixin.qq.com/sns/jscode2session?appid=wxc74d07edfe8a3490"
                    + "&secret=5e786af560a9df81bb6d4d89bcd85966" + "&js_code=" + code + "&grant_type=authorization_code");
            JSONObject jsonObject = JSON.parseObject(result);
            Object openid = jsonObject.get("openid");
            return StringUtils.isEmpty((CharSequence) openid)?"":openid.toString();
        }catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }
}
